package chenji.StrongFossil.Inventory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import chenji.StrongFossil.Main;
import chenji.StrongFossil.Fossil.PuncherFossil.PunchFossil;
import chenji.StrongFossil.Fossil.PuncherFossil.PuncherStrongType;

/**
 * 我相信看了这个类的源码的人一定会想打死我QAQ QAQ QAQ QWQ
 * 
 * @author ChenJi
 *
 */
public class PunchInventory {
	private Inventory inv;
	private Player p;
	private PunchInvState State;

	public enum PunchInvState {
		PunchItemNull, PunchAmountOut, PunchItemcannot, isnotPunchFossil, PunchFossilAmountOut, TheSameType, PuncherisEmpty, isnotPuncher, DurableOut, Arrow;
	}

	public enum PunchFossilState {
		L1NullL2notNull, L1notNullL2Null, L1L2Null, L1L2notNull;
	}

	/**
	 * 返回对应的Inventory
	 * 
	 * @return
	 */
	public Inventory getInventory() {
		return this.inv;
	}

	/**
	 * 让绑定的玩家打开Inventory
	 */
	public void openInventory() {
		this.p.closeInventory();
		this.p.openInventory(this.inv);
	}

	/**
	 * 清理资源
	 */
	public void closeInventory() {
		Main.getPunchManager().close(this);
	}

	/**
	 * 构造函数
	 * 
	 * @param p
	 */
	public PunchInventory(Player p) {
		this.p = p;
		inv = Bukkit.createInventory(null, 54, "打孔");
		ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
		ItemMeta m = i.getItemMeta();
		m.setDisplayName(" ");
		i.setItemMeta(m);
		int[] list = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 18, 26, 27, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
				45, 53 };
		for (int ia : list) {
			inv.setItem(ia, i);
		}
		i = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 9);
		m = i.getItemMeta();
		m.setDisplayName(" ");
		LinkedList<String> lore = new LinkedList<String>();
		lore.add("§c被打孔工具槽");
		lore.add("§c请将被打孔的物品放到中间的空位上");
		m.setLore(lore);
		i.setItemMeta(m);
		list = new int[] { 10, 11, 12, 19, 21, 28, 29, 30 };
		for (int ia : list) {
			inv.setItem(ia, i);
		}

		i = new ItemStack(Material.IRON_FENCE, 1, (short) 2);
		m = i.getItemMeta();
		m.setDisplayName(" ");
		lore = new LinkedList<String>();
		lore.add(" ");
		m.setLore(lore);
		i.setItemMeta(m);
		list = new int[] { 13, 31 };
		for (int ia : list) {
			inv.setItem(ia, i);
		}

		i = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 0);
		m = i.getItemMeta();
		m.setDisplayName(" ");
		lore = new LinkedList<String>();
		lore.add("§c请将打孔钻头放置与左侧空位");
		m.setLore(lore);
		i.setItemMeta(m);
		inv.setItem(50, i);

		i = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 0);
		m = i.getItemMeta();
		m.setDisplayName(" ");
		lore = new LinkedList<String>();
		lore.add("§c请将打孔钻头放置与右侧空位");
		lore.add("§c请将打孔镇定剂放置与左侧空位（可选）");
		m.setLore(lore);
		i.setItemMeta(m);
		inv.setItem(48, i);

		i = new ItemStack(Material.ANVIL, 1, (short) 2);
		m = i.getItemMeta();
		m.setDisplayName("介绍");
		lore = new LinkedList<String>();
		lore.add("§c你可以在这里为你的武器/工具打孔");
		lore.add("§c请在指定的位置放入要打孔的物品");
		lore.add("§c以及打孔钻头和镇定剂（可选）");
		lore.add("§c手持物品输入/Stf See即可查询");
		lore.add("§c手中物品的信息");
		lore.add("§c打孔不一定百分之百成功，可以看");
		lore.add("§c右下角指示灯显示的成功几率，加");
		lore.add("§c入镇定剂或使用更高品质的钻头成");
		lore.add("§c功率更高哦~");
		lore.add("§c打孔后可用于宝石镶嵌。");
		m.setLore(lore);
		i.setItemMeta(m);
		inv.setItem(22, i);

		i = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5);
		m = i.getItemMeta();
		m.setDisplayName(" ");
		lore = new LinkedList<String>();
		lore.add("§c成品槽");
		lore.add("§c打孔成功的物品将会放在这里");
		m.setLore(lore);
		i.setItemMeta(m);
		list = new int[] { 14, 15, 16, 23, 25, 32, 33, 34 };
		for (int ia : list) {
			inv.setItem(ia, i);
		}

		i = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 14);
		m = i.getItemMeta();
		m.setDisplayName(" ");
		lore = new LinkedList<String>();
		lore.add("§a请放入物品后点击这里开始合成");
		m.setLore(lore);
		i.setItemMeta(m);
		list = new int[] { 52, 51 };
		for (int ia : list) {
			inv.setItem(ia, i);
		}
		Main.getPunchManager().registerPunchInventory(p, this);
		p.openInventory(inv);
	}

	/**
	 * 刷新当前状态
	 */

	public void refreshState() {
		if (inv.getItem(20) == null) {
			this.State = PunchInvState.PunchItemNull;
			refreshInventory();
			return;
		}
		if (inv.getItem(20).getType() == Material.AIR) {
			this.State = PunchInvState.PunchItemNull;
			refreshInventory();
			return;
		}
		if (!(Main.getPunchManager().canPunch(inv.getItem(20).getType()))) {
			this.State = PunchInvState.PunchItemcannot;
			refreshInventory();
			return;
		}
		if (Main.getPunchManager().getPunchNumber(inv.getItem(20)) >= Main.getPunchManager()
				.getMaxPunchNumber(inv.getItem(20).getType())) {
			this.State = PunchInvState.PunchAmountOut;
			refreshInventory();
			return;
		}
		ArrayList<ItemStack> ia = new ArrayList<ItemStack>();
		if (!(isEmpty(inv.getItem(47)))) {
			ia.add(inv.getItem(47));
		}
		if (!(isEmpty(inv.getItem(46)))) {
			ia.add(inv.getItem(46));
		}
		if (ia.size() != 0) {
			if (ia.size() == 1) {
				ItemStack i = ia.get(0);
				if (!(Main.getPunchFossilManager().isPunchFossil(i))) {
					this.State = PunchInvState.isnotPunchFossil;
					refreshInventory();
				}
			}
			if (ia.size() == 2) {
				PuncherStrongType firstp = null;
				for (ItemStack ib : ia) {
					if (!(Main.getPunchFossilManager().isPunchFossil(ib))) {
						this.State = PunchInvState.isnotPunchFossil;
						refreshInventory();
						return;
					}
					if (firstp == null) {
						firstp = Main.getPunchFossilManager().getPunchFossil(ib).getType();
					} else {
						if (firstp == Main.getPunchFossilManager().getPunchFossil(ib).getType()) {
							this.State = PunchInvState.TheSameType;
							refreshInventory();
							return;
						}
					}

				}
			}
		}
	}

	private boolean isEmpty(ItemStack i) {
		if (i == null) {
			return true;
		}
		if (i.getType().equals(Material.AIR)) {
			return true;
		}
		return false;
	}

	public PunchInvState getState() {
		return this.State;
	}

	/**
	 * 刷新Inventory
	 */
	private void refreshInventory() {

		switch (this.State) {
		case PunchAmountOut: {
			this.operateInventory(true, "§c你所放入的物品已经达到了打孔上限");
			return;
		}
		case PunchItemNull: {
			this.operateInventory(true, "§c你必须放入被打孔的物品");
			return;
		}
		case PunchItemcannot: {
			this.operateInventory(true, "§c你所放入的物品不支持打孔");
			return;
		}
		case isnotPunchFossil: {
			this.operateInventory(true, "§c你所放入的镇定剂类型不对");
			return;
		}
		case PunchFossilAmountOut: {
			this.operateInventory(true, "§c镇定剂的数量只能放入一个");
			return;
		}
		case TheSameType: {
			this.operateInventory(true, "§c两个镇定剂的强化类型不能相同");
			return;
		}

		case PuncherisEmpty: {
			this.operateInventory(true, "§c你必须放入打孔钻头");
			return;
		}

		case isnotPuncher: {
			this.operateInventory(true, "§c你所放入的打孔钻头类型不对");
			return;
		}

		case DurableOut: {
			this.operateInventory(true, "§c打孔钻头的耐久不足以将这个物品打孔");
			return;
		}
		case Arrow: {
			if (this.State == PunchInvState.Arrow) {
				this.punch();
				this.operateInventory(false, "§a打孔成功");
				this.State = PunchInvState.PunchItemNull;
				return;
			}
			this.operateInventory(false, "§a可以打孔，点击打孔。");
			return;
		}
		}
	}

	private void operateInventory(boolean isRed, String Message) {
		ItemStack i;
		if (isRed) {
			i = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 14);
		} else {
			i = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5);
		}
		ItemMeta m = i.getItemMeta();
		ArrayList<String> lore = new ArrayList<String>();
		lore.add(Message);
		if (isRed) {
			lore.add("§c无法打孔，§c点击刷新。");
		} else {
			lore.addAll(this.getStrongState());
		}
		m.setLore(lore);
		m.setDisplayName("  ");
		i.setItemMeta(m);
		this.inv.setItem(52, i);
		this.inv.setItem(51, i);
	}

	private void DropIteminSlot(int slot) {
		if (this.inv.getItem(slot) != null) {
			this.p.getLocation().getWorld().dropItem(this.p.getLocation(), this.inv.getItem(slot));
		}
	}

	public void DropItem() {
		DropIteminSlot(20);
		DropIteminSlot(24);
		DropIteminSlot(49);
		DropIteminSlot(47);
		DropIteminSlot(46);
		this.inv.clear();
	}

	private void punch() {
		ItemStack ia = Main.getPunchManager().Punch(this.inv.getItem(20).clone());
		this.inv.setItem(24, ia);
		this.inv.setItem(46, null);
		this.inv.setItem(47, null);
		this.inv.setItem(20, null);
	}

	private Collection<String> getStrongState() {
		ArrayList<String> lore = new ArrayList<String>();
		lore.add(" ");
		Material m = this.inv.getItem(20).getType();
		int nowPunch = Main.getPunchManager().getPunchNumber(this.inv.getItem(20));
		lore.add("&c&l&m      &6&l&m      §c打孔信息&b&l&m      &c&l&m      ".replaceAll("&", "§"));
		if (nowPunch == 0) {
			lore.add("§a这个物品目前§c没有§a强化位");
		} else {
			lore.add("§a这个物品目前拥有 §c" + nowPunch + " §a个强化位");
			lore.add("§a其中有 §c" + Main.getPunchManager().getEmptyPunchNumber(this.inv.getItem(20)) + " §a个位置是空的");
		}
		lore.add("§a打孔后将会增加 §c1 §a个§b空位§a可用于宝石§e镶嵌");
		PunchFossil f1 = this.getPuncherFossil(PuncherStrongType.Improve);
		if (f1 != null) {
			lore.add("§a当前§b成功率§a为 §c" + Main.getPunchManager().getFinalChange(f1, nowPunch, m) + "% §b(+§c"
					+ f1.getValue() + "§b)");
		} else {
			lore.add("§a当前§b成功率§a为 §c" + Main.getPunchManager().getFinalChange(m, nowPunch) + "%");
		}
		f1 = this.getPuncherFossil(PuncherStrongType.Protect);
		lore.add("§a打孔工具的§b耐久值§a将会§e降低 §c" + Main.getPunchManager().getDurableDrop(m, nowPunch, f1) + " §a点");
		double b = Main.getPunchManager().getBrokeChange(nowPunch, m);
		if (b != 0D) {
			lore.add("§a请注意，打孔§b可能§a会有 §c " + b + "% §a的几率§c损坏§a这个物品。");
		}
		lore.add(Main.getPunchManager().getFinalSubject());
		return lore;
	}

	private PunchFossil getPuncherFossil(PuncherStrongType p) {
		ItemStack i1 = this.inv.getItem(47);
		ItemStack i2 = this.inv.getItem(46);
		if (Main.getPunchFossilManager().isPunchFossil(i1)) {
			PunchFossil f = Main.getPunchFossilManager().getPunchFossil(i1);
			if (f.getType().equals(p)) {
				return f;
			}
		}

		if (Main.getPunchFossilManager().isPunchFossil(i2)) {
			PunchFossil f = Main.getPunchFossilManager().getPunchFossil(i2);
			if (f.getType().equals(p)) {
				return f;
			}
		}
		return null;

	}

	/**
	 * 获取当前绑定的Player实例
	 * 
	 * @return
	 */
	public Player getPlayer() {
		// TODO Auto-generated method stub
		return this.p;
	}
}
